package kernel_db.parser;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import kernel_db.db.DB;
import kernel_db.db.DB_Error;
import kernel_db.parser.ParserDB;


//this class is for debug only
public class ParserTester {

	static Date start = null, stop;
	static Date start1 = null, stop1;
	
	public static void main(String[] args) throws DB_Error {
		DB db = new DB("jdbc:mysql://mysqlsrv.cs.tau.ac.il:3306/DbMysql12", "DbMysql12", "daniel87",2012, 1000, 1000);
		ParserDB parser = new ParserDB(db, 1000, 1000);
		calculateElapsedTime(true);

//		calculateElapsedTime(true, "Changing Tables DB to MyISAM");
//		parser.changeTablesToMyISAM();
//		calculateElapsedTime(false, "Changing Tables DB to MyISAM");
		
//		calculateElapsedTime(true, "DELETED ALL DB DATA !!!");
//		parser.deleteAll();
//		calculateElapsedTime(false, "DELETED ALL DB DATA !!!");
		
//		parser.setMoviesFilePath("/specific/scratches/scratch/idanslon/movies.list");
//		parser.setActorsFilePath("/specific/scratches/scratch/idanslon/actors.list");
//		parser.setActressesFilePath("/specific/scratches/scratch/idanslon/actresses.list");
//		parser.setDirectorsFilePath("/specific/scratches/scratch/idanslon/directors.list");
//		parser.setCountriesFilePath("/specific/scratches/scratch/idanslon/countries.list");
//		parser.setGenresFilePath("/specific/scratches/scratch/idanslon/genres.list");
//		parser.setRatingsFilePath("/specific/scratches/scratch/idanslon/ratings.list");
		
//		if (parser.parseAllFiles()) {
			System.out.println();
			calculateElapsedTime(true, "Changing Tables DB to InnoDB");
			parser.changeTablesToInnoDB();
			calculateElapsedTime(false, "Changing Tables DB to InnoDB");
			System.out.println();
			
			calculateElapsedTime(true, "Finding the top movies and actors");
			parser.findTopMoviesAndActors();
			calculateElapsedTime(false, "Finding the top movies and actors");
			System.out.println();
			
			System.out.println();
			System.out.println("Parsed DB Successfully in " + calculateElapsedTime(false) + " !!!");
//		}
//		else {
//			System.out.println(); 
//			System.out.println("WTF?! DB Parser has failed after " + calculateElapsedTime(false));
//		}
		
		
		db.closeAllConnections();
		
	}

	
	private static void calculateElapsedTime(Boolean isStart, String strToPrint) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		
		if (isStart) {
			start1 = new Date();
			System.out.println("START:   " + strToPrint + "  at " + dateFormat.format(date));
		}
		else {
			stop1 = new Date();
			
			long l1 = start1.getTime();
			long l2 = stop1.getTime();
			long diff = l2 - l1;
	
			long secondInMillis = 1000;
			long minuteInMillis = secondInMillis * 60;
			long hourInMillis = minuteInMillis * 60;
	
			long elapsedHours = diff / hourInMillis;
			diff = diff % hourInMillis;
			long elapsedMinutes = diff / minuteInMillis;
			diff = diff % minuteInMillis;
			long elapsedSeconds = diff / secondInMillis;
			
			System.out.println("FINSHED: " + strToPrint + "  at " + dateFormat.format(date) +
					", Time Elapsed=" + elapsedHours + ":" + elapsedMinutes + ":" + elapsedSeconds);
			System.out.println();
		}

	}
	private static String calculateElapsedTime(Boolean isStart) {
		if (isStart) {
			start = new Date();
			return "";
		}
		else {
			if (start == null)
				return "";
			
			stop = new Date();
			
			long l1 = start.getTime();
			long l2 = stop.getTime();
			long diff = l2 - l1;
	
			long secondInMillis = 1000;
			long minuteInMillis = secondInMillis * 60;
			long hourInMillis = minuteInMillis * 60;
	
			long elapsedHours = diff / hourInMillis;
			diff = diff % hourInMillis;
			long elapsedMinutes = diff / minuteInMillis;
			diff = diff % minuteInMillis;
			long elapsedSeconds = diff / secondInMillis;
			
			start = null;
			return elapsedHours + ":" + elapsedMinutes + ":" + elapsedSeconds;
		}

	}
}
